<template>
    <el-row class="lives-news" id="articleList">
        <el-row>
            <el-col class="title">
                <h3>公众号平台</h3>
            </el-col>
        </el-row>

        <el-row>
            <el-form :inline="true" class="inline-form">
                <el-form-item>
                    <router-link to="/articleAdd"><el-button type="primary"><i class="el-icon-plus el-icon--left"></i>添加文章</el-button></router-link>
                </el-form-item>
                <el-form-item label="文章标题">
                    <el-input placeholder="请输入文章标题" v-model="articleTitle">
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button size="small" type="primary" @click="getArticleLists">搜索</el-button>
                    <el-button size="small" type="danger" @click="emptySearch">清空</el-button>
                </el-form-item>
            </el-form>
        </el-row>
        <el-row>
            <el-col>
                <el-table :data="articleLists">
                    <el-table-column prop="articleId" label="文章ID" width="240px" align="center"></el-table-column>
                    <el-table-column prop="articleCategoryName" label="文章分类" width="200px" align="center"></el-table-column>
                    <el-table-column prop="articleTitle" label="文章标题" align="center"></el-table-column>
                    <el-table-column label="发布时间" align="center">
                        <template slot-scope="scope">{{moment(scope.row.time).format('YYYY-MM-DD HH:mm:ss')}}</template>
                    </el-table-column>
                    <el-table-column label="操作" width="160px" align="center">
                        <template slot-scope="scope">
                            <el-button size="small" @click="gotoUpdateArticlePage(scope.$index, scope.row)">修改</el-button>
                            <el-button size="small" type="danger" @click="deleteArticle(scope.$index, scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>

        <!-- 分页 -->
        <el-row>
            <el-col class="pagination">
                <el-pagination @current-change="currentChange" :current-page="pageId" :page-size="pageSize" layout="total, prev, pager, next" :total="counts">
                </el-pagination>
            </el-col>
        </el-row>

    </el-row>
</template>
<script>
import {
    getArticleList,
    addArticle,
    deleteArticleById,
    getArticleById,
    updateArticleById,
    getArticleCategoryList

} from '@/api/api'
export default {
    data: function() {
        return {
            formInline: {
                articleId: 0,
                articleCategoryId: '',
                articleTitle: '',
                articleContent: ''
            },
            articleTitle: '',
            pageId: 1,
            pageSize: 10,
            counts: 0,
            articleLists: null
        }
    },
    created: function() {
        this.pageId = parseInt(this.$route.query.page) || 1;
        this.articleTitle = this.$route.query.articleTitle || '';
        this.getArticleLists();
    },
    methods: {
        //获取视频分类列表
        getArticleLists: function() {
            var params = {
                pageId : this.pageId,
                articleTitle: this.articleTitle,
                articleCategoryType: 'discuss'
            }
            
            getArticleList({ params: params }).then(data => {
                var str = '?';
                for (var key in params) {
                    if (params[key]) {
                        str += key + '=' + params[key] + '&'
                    }
                }
                this.$router.push(str);
                this.counts = data.count;
                this.articleLists = data.list;
            })
        },
        gotoUpdateArticlePage: function(index, row){
            this.$router.push({path: '/articleUpdate',query: {articleId : row.articleId}})
        },
        //搜索
        searchArticleByName: function() {
            this.getArticleLists();
        },
        //分页
        currentChange: function(val) {
            this.$router.push('?page=' + val)
            this.pageId = val;
            this.getArticleLists()
        },
        deleteArticle: function(index, row){
            this.$confirm('是否删除该文章?', '删除文章', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteArticleById(row.articleId).then(() => {
                    this.getArticleLists();
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        emptySearch: function(){
            this.articleTitle = '';
            this.getArticleLists();
        }
    }
}
</script>
<style scoped lang="scss">
.lives-news {
    .title {
        font-size: 12px;
        border-bottom: 1px solid #eaeaea;
        margin-bottom: 5px;
        h3 {
            display: inline-block;
            padding: 5px 0;
            margin: 0;
            font-weight: normal;
            color: #23b7e5;
            border-bottom: 1px solid #23b7e5;
        }
    }
}
</style>